﻿using Microsoft.AspNetCore.Mvc;
using RBAC_Application.Dto.Input;
using RBAC_Application.Dto.Output;
using RBAC_Application.Dto.批量分配;
using RBAC_Application.Repository.Interface;
using RBAC_Application.Service.RBAC;
using Standard;

namespace B.MES.Login.Controllers
{
    /// <summary>
    /// 角色管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class RoleController : ControllerBase
    {
        private readonly IRbacService _rbacService;

        public RoleController(IRbacService rbacService)
        {
            _rbacService = rbacService;
        }

        /// <summary>
        /// 角色列表查询
        /// </summary>
        [HttpGet]
        public ApiResult<List<RoleDto>> GetRoles([FromQuery] string keyword = null)
        {
            try
            {
                var roles = _rbacService.GetRolesByCondition(keyword);
                return new ApiResult<List<RoleDto>>
                {
                    Code = ApiCode.Success,
                    ResultData = roles,
                    Message = "获取成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<List<RoleDto>>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }

        /// <summary>
        /// 创建角色
        /// </summary>
        [HttpPost]
        public ApiResult<int> CreateRole([FromBody] CreateRoleInput input)
        {
            try
            {
                _rbacService.CreateRole(input);
                return new ApiResult<int>
                {
                    Code = ApiCode.Success,
                    ResultData = 1,
                    Message = "创建成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<int>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }

        /// <summary>
        /// 更新角色
        /// </summary>
        [HttpPut]
        public ApiResult<int> UpdateRole([FromBody] UpdateRoleInput input)
        {
            try
            {
                _rbacService.UpdateRole(input);
                return new ApiResult<int>
                {
                    Code = ApiCode.Success,
                    ResultData = 1,
                    Message = "更新成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<int>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        [HttpDelete("{id}")]
        public ApiResult<int> DeleteRole(long id)
        {
            try
            {
                _rbacService.DeleteRole(id);
                return new ApiResult<int>
                {
                    Code = ApiCode.Success,
                    ResultData = 1,
                    Message = "删除成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<int>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }

        /// <summary>
        /// 检查角色名唯一性
        /// </summary>
        [HttpGet("exists")]
        public ApiResult<bool> CheckRoleNameExists([FromQuery] string roleName)
        {
            try
            {
                var exists = _rbacService.CheckRoleNameExists(roleName);
                return new ApiResult<bool>
                {
                    Code = ApiCode.Success,
                    ResultData = exists,
                    Message = exists ? "角色名已存在" : "角色名可用"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<bool>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }
        /// <summary>
        /// 获取角色结构
        /// </summary>
        [HttpGet("tree")]
        public ApiResult<List<RoleTreeNode>> GetRoleTree()
        {
            try
            {
                var tree = _rbacService.GetRoleTree();
                return new ApiResult<List<RoleTreeNode>>
                {
                    Code = ApiCode.Success,
                    ResultData = tree,
                    Message = "获取成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<List<RoleTreeNode>>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }
    }
}